<?php


function past_pages ($start, $number, $count, $url, $get_start_name = 'page', $after_url_arr = array(), $admin_panel = false) 
{	
	$g_start = $start;
	$pages = '';
	$p_count = ($start / $number) + 1;
	
	$after_url = '';
	// Обрабатываем список элементов поиска
	foreach ($after_url_arr as $element)
	{
		($after_url == '')
			? $after_url .= '?'
			: $after_url .= '&';
			
		$after_url .= $element;
	}
	
	if ($count > $number) 
	{
		if ($g_start > 0) 
		{ 
			($admin_panel == true) 
				? $pages .= '<a href="'.$url.'page='.( ($g_start - $number) / $number + 1).'">&#8592;</a>' 
				: $pages .= '<a href="'.$url.$get_start_name.(($g_start - $number) / $number + 1).'/'.$after_url.'">&#8592;</a>'; 
		}
		
		if ($start - $number >= 0) 
		{
			$start_back = $start;
			
			$p_count_back = 1;
			$back_s = 0 - $number;
			$break = $start_back / $number;
			$i = 1;
			
			if ($start_back / $number >= 5)
			{
				($admin_panel == true)	
					? $pages .= '<a href="'.$url.'page=1">1</a>' 
					: $pages .= '<a href="'.$url.$get_start_name.'1/'.$after_url.'">1</a> ... ';
				$back_s = $number - ($number - 4);
				$p_count_back = $start_back / $number - 3;
				$break = 5;
			}
			
			while ($start_back - $number >= 0) 
			{
				if ($break == 5) 
					if ($i >= $break) break;
				else 
					if ($i > $break) break;
				
				if ($back_s + $number >= $start) break;
				
				if ($break != 5) 
					($admin_panel == true) 
						? $pages .= '<a href="'.$url.'page='.(($back_s + $number) / $number + 1).'">'.$p_count_back.'</a>'
						: $pages .= '<a href="'.$url.$get_start_name.(($back_s + $number)/$number + 1).'/'.$after_url.'">'.$p_count_back.'</a>';
				else 
					($admin_panel == true) 
						? $pages .= '<a href="'.$url.'page='.$p_count_back.'">'.$p_count_back.'</a>'
						: $pages .= '<a href="'.$url.$get_start_name.$p_count_back.'/'.$after_url.'">'.$p_count_back.'</a>';
				
				$p_count_back++;
				$back_s = $back_s + $number;
				$i++;
			}
		}
		$pages .= '<b>'.$p_count.'</b>';
		if ($start + $number < $count) 
		{	
			$break = ($count - $start) / $number;
			$i = 1;			
			
			if ($break > 4) $break = 5;
			
			while ($start + $number < $count) 
			{
				//if ($i >= $break) break;
				
				if ($start + $number > $count) break;
				$p_count++;
				if ($i < $break) ($admin_panel == true)
					? $pages .= '<a href="'.$url.'page='.(($start + $number) / $number+ 1).'">'.$p_count.'</a>'
					: $pages .= '<a href="'.$url.$get_start_name.(($start + $number) / $number + 1).'/'.$after_url.'">'.$p_count.'</a>';
				
				$last_page = (($start + $number) / $number ) + 1;
				$start = $start + $number;
				$i++;
			}
			if ($break == 5) 
				($admin_panel == true) 
					? $pages .= ' ... <a href="'.$url.'page='.$last_page.'">'.$last_page.'</a>'
					: $pages .= ' ... <a href="'.$url.$get_start_name.$last_page.'/'.$after_url.'">'.$last_page.'</a>';
		}
		if ($count > $g_start + $number) 
		{ 
			($admin_panel == true) 
				? $pages .= '<a href="'.$url.'page='.(($g_start + $number) / $number + 1).'">&#8594;</a>' 
				: $pages .= '<a href="'.$url.$get_start_name.(($g_start + $number) / $number + 1).'/'.$after_url.'">&#8594;</a>'; 
		}
		
		($admin_panel == true) 
			? $return = '<center>Всего: '.$count.', Страницы: '.$pages.'</center>' 
			: $return = '<div class="pages">Страницы: '.$pages.'</div>';
		return $return;
	}
}





?>
